<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
  </ul>
  <script>
    var lis = document.getElementsByTagName('li');

    for (var i = 0; i < lis.length; i++) {
      (function (index) {
        lis[i].onclick = function () {
          console.log(index);
        }
      })(i)
    }


    // 闭包：函数嵌套形成闭包，内部函数引用外部函数中的变量，函数调用以后，变量不会被释放


    function outer(a) {
      /////
      // var a = 6
      function inner() {
        var b = a + 4
        console.log(b);
      }
      return inner

    }

    var c = outer(5)
    // var c = outer()
    c()

      // 函数立即调用
      // (function fn1(n) {
      //   console.log(n);
      // })(666)

  </script>
</body>

</html>